package com.scu.servlet;

import com.alibaba.fastjson.JSON;
import com.scu.dao.UserDao;
import com.scu.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;


//注解配置的就是该类的url地址
//http://ip:端口/abc
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doGet方法");
        UserDao userDao = new UserDao();
        try {

            List<User> list = userDao.findAllUser();

            String listStr = JSON.toJSONString(list);

            PrintWriter out = response.getWriter();
            out.print(listStr);
            out.flush();
            out.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doPost方法");
    }
}
